﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ivi.Visa;
using NationalInstruments.Visa;

namespace rmx4126
{
    public partial class Form1 : Form
    {
        public ResourceManager rm;
        public MessageBasedSession mbSession;
        //public IMessage msg;

        public Form1()
        {
            InitializeComponent(); 
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
            rm = new ResourceManager();
            // msg = rm.Open("TCPIP::169.254.7.8::INSTR", AccessMode.NO_LOCK , 0, "")  'Example: LAN
            mbSession = (MessageBasedSession)rm.Open("TCPIP::192.168.1.123::INSTR");
            mbSession.TerminationCharacterEnabled =true;
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //mbSession.RawIO.Write("*IDN?\\r\\n");
            mbSession.RawIO.Write("*IDN?\n");
            textBox1.Text= mbSession.RawIO.ReadString();
        }

        
        private void btnReadV_Click(object sender, EventArgs e)
        {
            mbSession.RawIO.Write(":VOLT?\n");
            txbV.Text = mbSession.RawIO.ReadString();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            mbSession.RawIO.Write(":CURR?\n");
            txbCur.Text = mbSession.RawIO.ReadString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            mbSession.RawIO.Write(":OUTP?\n");
            if (mbSession.RawIO.ReadString() == "1\n")
            {
                mbSession.RawIO.Write(":OUTP 0\n");
            }
            else
            {
                mbSession.RawIO.Write(":OUTP 1\n");
            }
        }

        private void btnSetV_Click(object sender, EventArgs e)
        {
            mbSession.RawIO.Write($":VOLT {txbSetV.Text}\n");
        }

        private void btnSetCur_Click(object sender, EventArgs e)
        {
            mbSession.RawIO.Write($":CURR {txbSetCur.Text}\n");
        }

        private void btnMeasure_Click(object sender, EventArgs e)
        {
            mbSession.RawIO.Write("MEAS:CURR?\n");
            label1.Text = "电流"+mbSession.RawIO.ReadString();
            mbSession.RawIO.Write("MEAS:VOLT?\n");
            label1.Text+=(" 电压"+mbSession.RawIO.ReadString());
        }
    }
}
